package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {
    
    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private NewsContentMapper contentMapper;

    public News selectByPrimaryKey(Long id) {
        return null;
    }

    public List<News> selectAll() {
        return null;
    }

    public PageInfo query(NewsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<News> news = newsMapper.queryForList(qo);
        return new PageInfo(news);
    }

   
    public void update(News news) {
        Date date = new Date();
        news.setReleaseTime(date);

        newsMapper.updateByPrimaryKey(news);

        if(news.getContent()!=null){
            NewsContent content = news.getContent();
            content.setId(news.getId());
            contentMapper.updateByPrimaryKey(content);
        }
        
    }

    public void save(News news) {
        Date date = new Date();
        news.setCreateTime(date);

        if(news.getIsRelease()){
            news.setReleaseTime(date);
        }


        newsMapper.insert(news);

        if(news.getContent()!=null){
            NewsContent content = news.getContent();
            content.setId(news.getId());
            contentMapper.insert(content);
        }

    }

    public News getContentById(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    public List<News> selectAllByKeyWord(String keyWord) {
        return newsMapper.selectAllByKeyWord(keyWord);
    }
}
